﻿using DSPL.Contracts;
using DSPL.Contracts.Common;
using DSPL.Contracts.Interfaces;
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DSPL.Common
{
    /// <summary>
    /// Common Textbox tool for handeling Currency Details
    /// </summary>
    public partial class TxtCurrency : UserControl
    {
        public TxtCurrency()
        {
            InitializeComponent();
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            ResolveDependancies();
        }

        #region Declaring Properties

        #region General Properties

        IMasterDataRetrieve MasterDataRetrieve;
 
        public object CurrencyKeyValue
        {
            get { return (object)GetValue(CurrencyKeyValueProperty); }
            set { SetValue(CurrencyKeyValueProperty, value); }
        }

        public string KeyFieldName
        {
            get { return (string)GetValue(KeyFieldNameProperty); }
            set { SetValue(KeyFieldNameProperty, value); }
        }

        public string KeyFieldType
        {
            get { return (string)GetValue(KeyFieldTypeProperty); }
            set { SetValue(KeyFieldTypeProperty, value); }
        }

        public DataRow SelectedDataRow
        {
            get { return (DataRow)GetValue(SelectedDataRowProperty); }
            set { SetValue(SelectedDataRowProperty, value); }
        }

        public Modes ParentFormMode
        {
            get { return (Modes)GetValue(ParentFormModeProperty); }
            set { SetValue(ParentFormModeProperty, value); }
        }

        public bool IsMandatory
        {
            get { return (bool)GetValue(IsMandatoryProperty); }
            set { SetValue(IsMandatoryProperty, value); }
        }

        #endregion

        #region txtCode Properties

        public Visibility CodeVisibility
        {
            get { return (Visibility)GetValue(CodeVisibilityProperty); }
            set { SetValue(CodeVisibilityProperty, value); }
        }

        public bool CodeEnable
        {
            get { return (bool)GetValue(CodeEnableProperty); }
            set { SetValue(CodeEnableProperty, value); }
        }

        public string CurrencyCode
        {
            get { return (string)GetValue(CurrencyCodeProperty); }
            set { SetValue(CurrencyCodeProperty, value); }
        }

        public int CodeColumnSpan
        {
            get { return (int)GetValue(CodeColumnSpanProperty); }
            set { SetValue(CodeColumnSpanProperty, value); }
        }

        #endregion

        #region txtDescription Properties

        public Visibility DescriptionVisibility
        {
            get { return (Visibility)GetValue(DescriptionVisibilityProperty); }
            set { SetValue(DescriptionVisibilityProperty, value); }
        }

        public bool DescriptionEnable
        {
            get { return (bool)GetValue(DescriptionEnableProperty); }
            set { SetValue(DescriptionEnableProperty, value); }
        }

        public string CurrencyDescription
        {
            get { return (string)GetValue(CurrencyDescriptionProperty); }
            set { SetValue(CurrencyDescriptionProperty, value); }
        }
        
        public int DescriptionColumnSpan
        {
            get { return (int)GetValue(DescriptionColumnSpanProperty); }
            set { SetValue(DescriptionColumnSpanProperty, value); }
        }

        public int DescriptionColumn
        {
            get { return (int)GetValue(DescriptionColumnProperty); }
            set { SetValue(DescriptionColumnProperty, value); }
        }

        public bool IsFocus
        {
            get { return (bool)GetValue(IsFocusProperty); }
            set { SetValue(IsFocusProperty, value); }
        }

        #endregion

        #region Other Currency Details Properties

        public double CurrencyRate
        {
            get { return (double)GetValue(CurrencyRateProperty); }
            set { SetValue(CurrencyRateProperty, value); }
        }

        

public double SalesRate
{
    get { return (double)GetValue(SalesRateProperty); }
    set { SetValue(SalesRateProperty, value); }
}





        #endregion

        #endregion

        #region Exposable Event Handlers

        public event EventHandler<EventArgs> RefreshCurrencyRelatedData;             //use to data validate before internal method execution, when data saving

        #endregion


        #region Dependency Properties

        public static readonly DependencyProperty CurrencyKeyValueProperty = DependencyProperty.Register("CurrencyKeyValue", typeof(object), typeof(TxtCurrency), new PropertyMetadata(null));

        public static readonly DependencyProperty KeyFieldNameProperty = DependencyProperty.Register("KeyFieldName", typeof(string), typeof(TxtCurrency), new PropertyMetadata(""));

        public static readonly DependencyProperty KeyFieldTypeProperty = DependencyProperty.Register("KeyFieldType", typeof(string), typeof(TxtCurrency), new PropertyMetadata(null));

        public static readonly DependencyProperty SelectedDataRowProperty = DependencyProperty.Register("SelectedDataRow", typeof(DataRow), typeof(TxtCurrency), new PropertyMetadata(null));

        public static readonly DependencyProperty CodeVisibilityProperty = DependencyProperty.Register("CodeVisibility", typeof(Visibility), typeof(TxtCurrency), new PropertyMetadata(Visibility.Visible));

        public static readonly DependencyProperty CodeEnableProperty = DependencyProperty.Register("CodeEnable", typeof(bool), typeof(TxtCurrency), new PropertyMetadata(true));

        public static readonly DependencyProperty CurrencyCodeProperty = DependencyProperty.Register("CurrencyCode", typeof(string), typeof(TxtCurrency), new PropertyMetadata(""));

        public static readonly DependencyProperty DescriptionVisibilityProperty = DependencyProperty.Register("DescriptionVisibility", typeof(Visibility), typeof(TxtCurrency), new PropertyMetadata(Visibility.Visible));

        public static readonly DependencyProperty DescriptionEnableProperty = DependencyProperty.Register("DescriptionEnable", typeof(bool), typeof(TxtCurrency), new PropertyMetadata(false));

        public static readonly DependencyProperty CurrencyDescriptionProperty = DependencyProperty.Register("CurrencyDescription", typeof(string), typeof(TxtCurrency), new PropertyMetadata(""));

        public static readonly DependencyProperty ParentFormModeProperty = DependencyProperty.Register("ParentFormMode", typeof(Modes), typeof(TxtCurrency), new PropertyMetadata(Modes.Undo));

        public static readonly DependencyProperty IsMandatoryProperty = DependencyProperty.Register("IsMandatory", typeof(bool), typeof(TxtCurrency), new PropertyMetadata(false));

        public static readonly DependencyProperty CodeColumnSpanProperty = DependencyProperty.Register("CodeColumnSpan", typeof(int), typeof(TxtCurrency), new PropertyMetadata(1));

        public static readonly DependencyProperty DescriptionColumnSpanProperty = DependencyProperty.Register("DescriptionColumnSpan", typeof(int), typeof(TxtCurrency), new PropertyMetadata(1));

        public static readonly DependencyProperty DescriptionColumnProperty = DependencyProperty.Register("DescriptionColumn", typeof(int), typeof(TxtCurrency), new PropertyMetadata(1));

        public static readonly DependencyProperty IsFocusProperty = DependencyProperty.Register("IsFocus", typeof(bool), typeof(TxtCurrency), new PropertyMetadata(false));

        public static readonly DependencyProperty CurrencyRateProperty = DependencyProperty.Register("CurrencyRate", typeof(double), typeof(TxtCurrency), new PropertyMetadata(0.00));

        public static readonly DependencyProperty SalesRateProperty =     DependencyProperty.Register("SalesRate", typeof(double), typeof(TxtCurrency), new PropertyMetadata(0.00));

        #endregion


        #region Internal Events

        private void RefreshAttachedObjects(object sender, EventArgs e)
        {
            CurrencyRate = 1.00;
            SalesRate = 1.00;

            if (GlobalVariables.SystemCurrency.ToUpper() == "LOCAL")
            {
                CurrencyKeyValue = (int)(MasterDataRetrieve.GetCurrencyKey(GlobalVariables.BaseCurrency));

            }
            else
            {
                if (CurrencyCode != GlobalVariables.BaseCurrency)
                {
                    DataTable dtCurrency = MasterDataRetrieve.GetCurrencyRate((int)CurrencyKeyValue);
                    if (dtCurrency != null && dtCurrency.Rows.Count > 0)
                    {
                        CurrencyRate = (double)dtCurrency.Rows[0]["CurRate"];
                        SalesRate = (double)dtCurrency.Rows[0]["SelRate"];
                    }
                }
                
            }
            if (RefreshCurrencyRelatedData!=null)
                RefreshCurrencyRelatedData(sender, e);
        }

        private void ResolveDependancies()
        {
            IUnityContainer container = GlobalVariables.GlobalUnityContainer;
            if (container != null)
            {
                MasterDataRetrieve = container.Resolve<IMasterDataRetrieve>();
            }
        }
        #endregion
    }
}
